//package com.bean.qq.service;
//
//
//
//import com.bean.qq.config.KieServerProperties;
//import com.bean.qq.entity.Order;
//import com.bean.qq.util.ErrorCode;
//import com.bean.qq.util.ServiceExceptionUtil;
//import org.kie.api.KieServices;
//import org.kie.api.command.Command;
//import org.kie.api.command.KieCommands;
//import org.kie.api.runtime.ExecutionResults;
//import org.kie.server.api.marshalling.MarshallingFormat;
//import org.kie.server.api.model.KieServiceResponse;
//import org.kie.server.api.model.ServiceResponse;
//import org.kie.server.client.KieServicesClient;
//import org.kie.server.client.KieServicesConfiguration;
//import org.kie.server.client.KieServicesFactory;
//import org.kie.server.client.RuleServicesClient;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.PostConstruct;
//import java.util.ArrayList;
//import java.util.LinkedList;
//import java.util.List;
//
///**
// * 远程kie-server相关服务
// *
// * @author
// * @date 2023/9/6 13:51
// **/
//@Service
//public class KieClientService {
//
//    private static final Logger log = LoggerFactory.getLogger(KieClientService.class);
//
//    @Autowired
//    private KieServerProperties kieServerProperties;
//
//    /**
//     * 获取命令对象
//     *
//     * @return 命令对象
//     */
//    public KieCommands getKieCommands() {
//        return KieServices.Factory.get().getCommands();
//    }
//
//    /**
//     * 获取存放命令的LinkedList
//     *
//     * @return 存放命令的LinkedList
//     */
//    public List<Command<?>> getKieCommandList() {
//        return new LinkedList<>();
//    }
//
//    /**
//     * 执行规则
//     *
//     * @param commandList 存放命令的LinkedList
//     * @param kieCommands 命令对象
//     * @return 执行结果
//     */
//    public ExecutionResults executeCommands(List<Command<?>> commandList, KieCommands kieCommands) {
//        RuleServicesClient client = this.getClient();
//        ServiceResponse<ExecutionResults> serviceResponse = client.executeCommandsWithResults(kieServerProperties.getContainerId(),
//            kieCommands.newBatchExecution(commandList, kieServerProperties.getSession()));
//        if (serviceResponse == null || !KieServiceResponse.ResponseType.SUCCESS.equals(serviceResponse.getType())) {
//            log.error("调用kie-server失败");
//            throw new ServiceExceptionUtil(new ErrorCode(500, "调用kie-server失败"));
//        }
//        return serviceResponse.getResult();
//    }
//
//    /**
//     * 根据配置获取连接
//     *
//     * @return 与kie-server的连接
//     */
//    private RuleServicesClient getClient() {
//        //配置信息
//        KieServicesConfiguration kieServicesConfiguration = KieServicesFactory.newRestConfiguration(
//            kieServerProperties.getUrl(), kieServerProperties.getUsername(),
//            kieServerProperties.getPassword(), Long.parseLong(kieServerProperties.getTimeout()));
//        kieServicesConfiguration.setMarshallingFormat(MarshallingFormat.JSON);
//
//        RuleServicesClient servicesClient;
//        try {
//            //创建规则服务客户端
//            KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(kieServicesConfiguration);
//            servicesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
//        } catch (Exception exception) {
//            log.error(exception.getMessage());
//            throw new ServiceExceptionUtil(new ErrorCode(500, "无法建立到kie-server的连接"));
//        }
//        if (servicesClient == null) {
//            log.error("servicesClient为null");
//            throw new ServiceExceptionUtil(new ErrorCode(500, "无法建立到kie-server的连接"));
//        }
//        return servicesClient;
//    }
//
//    /**
//     * 将list插入kie-server工作内存并处理
//     *
//     * @param list 要插入的数据列表
//     * @param <T>  数据的类型
//     * @return 原列表，内容已改变
//     */
//    public <T> List<T> processList(List<T> list) {
//        if (StringUtils.isEmpty(list)) {
//            return new ArrayList<>();
//        }
//        List<Command<?>> kieCommandList = this.getKieCommandList();
//        KieCommands kieCommands = this.getKieCommands();
//        for (int i = 0; i < list.size(); i++) {
//            kieCommandList.add(kieCommands.newInsert(list.get(i), String.valueOf(i)));
//        }
//        ExecutionResults executionResults = this.executeCommands(kieCommandList, kieCommands);
//        for (int i = 0; i < list.size(); i++) {
//            BeanUtils.copyProperties(executionResults.getValue(String.valueOf(i)), list.get(i));
//        }
//        return list;
//    }
//
//
//}
//
